package com.miracle9.game.dao;
import com.miracle9.game.entity.UserWarning;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

@Repository
public class UserWarningServiceDao extends SqlOperating<UserWarning, Integer>
{
  public List<UserWarning> getUserWarnings()
  {
    return q_CreateSqlList("from UserWarning", new Object[0]);
  }
  
  public UserWarning getByUid(int nUserId)
  {
    return (UserWarning)q_CreateSqlFilter("from UserWarning where userId=?", new Object[] { Integer.valueOf(nUserId) });
  }
  
  public int updateDeductInfoByUid(int userId, long minusGold)
  {
    getSession().flush();
    getSession().clear();
    List<Object> params = new ArrayList();
    if (minusGold < 0L) {
      return -1;
    }
    String hql = "update UserWarning set deductGold = deductGold+?,gameGold = gameGold-? where userId = ?";
    params.add(Long.valueOf(minusGold));
    params.add(Long.valueOf(minusGold));
    params.add(Integer.valueOf(userId));
    return q_CreateSql(hql, params.toArray()).executeUpdate();
  }
  
  public int updateGiveInfoByUid(int userId, long giveGold)
  {
    getSession().flush();
    getSession().clear();
    List<Object> params = new ArrayList();
    if (giveGold < 0L) {
      return -1;
    }
    String hql = "update UserWarning set giveGold = giveGold+?,gameGold = gameGold+? where userId = ?";
    params.add(Long.valueOf(giveGold));
    params.add(Long.valueOf(giveGold));
    params.add(Integer.valueOf(userId));
    return q_CreateSql(hql, params.toArray()).executeUpdate();
  }
  
  public int updateExpiryCountByUid(int userId, long gold)
  {
    getSession().flush();
    getSession().clear();
    List<Object> params = new ArrayList();
    if (gold < 0L) {
      return -1;
    }
    String hql = "update UserWarning set expiryCount = expiryCount+?,gameGold = gameGold+?  where userId = ?";
    params.add(Long.valueOf(gold));
    params.add(Long.valueOf(-gold));
    params.add(Integer.valueOf(userId));
    return q_CreateSql(hql, params.toArray()).executeUpdate();
  }
  
  public int updatePayCountByUid(int userId, long gold)
  {
    getSession().flush();
    getSession().clear();
    List<Object> params = new ArrayList();
    if (gold < 0L) {
      return -1;
    }
    String hql = "update UserWarning set payCount = payCount+?,gameGold = gameGold+? where userId = ?";
    params.add(Long.valueOf(gold));
    params.add(Long.valueOf(gold));
    params.add(Integer.valueOf(userId));
    return q_CreateSql(hql, params.toArray()).executeUpdate();
  }
  
  public void updateGoldInfoByUid(int userId)
  {
	  q_CreateSql("update UserWarning set yaGold=0,deGold=0,giveGold=0,deductGold=0,payCount=0,expiryCount=0,gameGold=0 where userId=?", new Object[] { Integer.valueOf(userId) }).executeUpdate();
  }
  
  public UserWarning getLastUser()
  {
    Query query = q_CreateSql("from UserWarning order by id desc", new Object[0]);
    query.setMaxResults(1);
    return (UserWarning)query.uniqueResult();
  }
  
  public void deleteByNUserId(int nUserId)
  {
	  q_CreateSql("delete UserWarning where userId=?", new Object[] { Integer.valueOf(nUserId) }).executeUpdate();
  }
}
